<template>
  <div class="app-container">
    <div class="page-card top-tabs">
      <div class="page-back" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
        返回
      </div>
    </div>

    <div class="page-card m-top">
      <div class="page-form">
        <el-select
          v-hasRole="['admin']"
          v-model="queryParams.examSchoolId"
          placeholder="选择学校"
          clearable
          @change="handleSearch"
        >
          <el-option
            v-for="item in xxlb"
            :key="item.id"
            :label="item.schoolName"
            :value="item.id.toString()"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="queryParams.examSubjectId"
          placeholder="选择科目"
          clearable
          @change="handleSearch"
        >
          <el-option
            v-for="item in kmlb"
            :key="item.id"
            :label="item.name"
            :value="item.id.toString()"
          ></el-option>
        </el-select>
        <el-select
          v-model="tkzt"
          placeholder="题卡状态"
          clearable
          @change="handleSearch"
        >
          <el-option label="选项异常" value="abnormalOption"></el-option>
          <el-option label="后传题卡" value="passCard"></el-option>
        </el-select>
        <el-input
          v-model="queryParams.examScanDetails.admissionNo"
          placeholder="准考证号"
          clearable
          @input="handleSearch"
        />
        <el-input
          v-model="queryParams.examScanDetails.studentName"
          placeholder="姓名"
          clearable
          @input="handleSearch"
        />
      </div>
      <el-table
        stripe
        border
        ref="tableList"
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        row-key="id"
        v-show="showTable"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          :reserve-selection="true"
        />
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="学校名称"
          align="center"
          prop="examSchoolName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="姓名"
          align="center"
          prop="studentName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="科目"
          align="center"
          prop="examSubjectName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="准考证号"
          align="center"
          prop="admissionNo"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="班级"
          align="center"
          prop="examClassName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="考场号"
          align="center"
          prop="examRoomNo"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="座号"
          align="center"
          prop="seatNo"
          :show-overflow-tooltip="true"
        />

        <el-table-column
          label="题卡状态"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-tag
              type="danger"
              size="mini"
              v-if="
                scope.row.abnormalOption == '1' && scope.row.missingExam != 1
              "
              >选项异常</el-tag
            >
            <el-tag size="mini" v-if="scope.row.passCard == '1'"
              >后传题卡</el-tag
            >
            <el-tag
              type="success"
              size="mini"
              v-if="
                scope.row.passCard != '1' && scope.row.abnormalOption != '1'
              "
              >-</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="上传时间"
          align="center"
          prop="createTime"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="cktk(scope.row)">查看题卡</el-link>
            <el-link
              type="danger"
              v-hasRole="['admin']"
              @click="del(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="img-list" v-show="!showTable">
        <el-checkbox-group class="list" v-model="ids" @change="checkChange">
          <div v-for="item in tableData" :key="item.id">
            <el-checkbox :label="item.id">{{ item.studentName }}</el-checkbox>
            <div class="img-item" @click="cktk(item)">
              <img :src="item.minFrontImgPath" alt="" />
              <img :src="item.minBackImgPath" alt="" />
            </div>
          </div>
        </el-checkbox-group>
      </div>
    </div>

    <div class="page-bottom">
      <div class="sel-box">
        <p>
          已选 <span>{{ ids.length }}</span> 项 / 共 {{ total }} 项
        </p>
        <el-button
          v-hasRole="['admin']"
          type="danger"
          :disabled="tableData.length == 0"
          @click="xxyccl"
          >选项异常处理</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="primary"
          :disabled="tableData.length == 0"
          @click="clcz"
          >处理操作</el-button
        >
        <el-button type="primary" @click="showTable = !showTable">{{
          showTable ? "图片模式" : "列表模式"
        }}</el-button>
        <el-button
          v-hasRole="['admin']"
          type="danger"
          :disabled="multiple"
          @click="del(false)"
          >批量删除</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="danger"
          @click="allDel(false)"
          >全部删除</el-button
        >
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imgList"
    />
  </div>
</template>

<script>
import { scanQuestionCardSchoolListSchool } from "@/api/xmgl/tkcx";
import {
  scanExamSubject,
  scanRecognitionRecord,
  scanDel,
  scanAlldelete
} from "@/api/xmgl/smgl";
import { debounce } from "lodash";
import { checkPassword } from "@/api/common";
export default {
  name: "Project_tkxq",
  components: {
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
  },
  data() {
    return {
      firstLoad: true,

      total: 0,
      ids: [],
      tableData: [],
      multiple: true,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        examSubjectId: this.$route.query.subjectId,
        examSchoolId: this.$route.query.examSchoolId,
        examScanDetails:{
          admissionNo:'',
          studentName:''
        }
      },
      xxlb: [],
      kmlb: [],
      imgViewerVisible: false,
      imgList: [],
      tkzt: "",

      showTable: true,
    };
  },
  created() {
    this.$store.dispatch("getXmxx").then((res) => {
      this.xmxx = res;
      scanExamSubject({ examId: this.xmxx.id }).then((res) => {
        this.kmlb = res.data;
      });
      scanQuestionCardSchoolListSchool({
        examId: this.xmxx.id,
      }).then((res) => {
        this.xxlb = res.rows;

        if (!this.$auth.hasRole("admin")) {
          this.queryParams.examSchoolId = this.xxlb.filter((item) => {
            return this.$store.state.user.deptId == item.schoolId;
          })[0].id;
        }
        this.getList();
      });
    });
  },
  activated() {
    if (!this.firstLoad) {
      if (this.$store.state.app.isRefresh) {
        this.queryParams.examSubjectId = this.$route.query.subjectId;
        this.queryParams.examSchoolId = this.$route.query.examSchoolId;
        this.getList(1);
      } else {
        this.getList();
      }
    }
    this.firstLoad = false;
    this.$store.dispatch("app/setIsRefresh", false);
  },

  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    debouncedSearch: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500),
    handleSearch() {
      this.debouncedSearch();
    },
    getList(page = null) {
      if (page) {
        this.queryParams.pageNum = page;
      }
      this.loading = true;
      this.tableData = [];
      let data = {
        ...this.queryParams,
        examId: this.xmxx.id,
        examSchoolId: this.queryParams.examSchoolId
          ? [this.queryParams.examSchoolId]
          : [],
      };
      delete data.examScanDetails.passCard;
      delete data.examScanDetails.abnormalOption;
      if (this.tkzt) {
        // data.examScanDetails = {};
        data.examScanDetails[this.tkzt] = 1;
      }

      scanRecognitionRecord(data)
        .then((response) => {
          this.total = response.data.total;
          this.tableData = response.data.records;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    cktk(row) {
      this.imgList = [row.frontImgPath, row.backImgPath];
      this.imgViewerVisible = true;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "hidden";
      document.addEventListener("touchmove", m, false); // 禁止页面滑动
    },
    closeImgViewer() {
      this.imgViewerVisible = false;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "auto";
      document.removeEventListener("touchmove", m, true);
    },
    xxyccl() {
      this.$router.push({
        path: "/kwgl/tkyccl/index",
        query: {
          examSchoolId: this.queryParams.examSchoolId,
          examSubjectId: this.queryParams.examSubjectId,
          templateId: this.tableData[0].templateId,
        },
      });
    },

    checkChange(value) {
      this.tableData.map((item) => {
        if (value.indexOf(item.id) != -1) {
          this.$refs.tableList.toggleRowSelection(item, true);
        } else {
          this.$refs.tableList.toggleRowSelection(item, false);
        }
      });
    },

    clcz() {
      let query = {
        examId: this.xmxx.id,
        examSchoolId: this.queryParams.examSchoolId,
        examSubjectId: this.queryParams.examSubjectId,
        templateId: this.tableData[0].templateId,
        tkzt: this.tkzt,
      };

      if (this.ids.length > 0) {
        query.ids = this.ids.join(",");
      }
      this.$router.push({
        path: "/kwgl/cltk/index",
        query: query,
      });
    },

    del(id) {
      this.$modal.confirm("请确认进行删除操作").then(() => {
        scanDel({ ids: id ? [id] : this.ids }).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.getList();
        });
      });
    },
    //全部删除
    allDel(){
      let abnormalOption=''
      let passCard=""
      if(this.tkzt!=''){
          if(this.tkzt=='abnormalOption'){
            abnormalOption=1
          }else{
            passCard=1
          }
      }
      
      let obj={
        examId: this.xmxx.id,
        examSchoolId: this.queryParams.examSchoolId,
        examSubjectId: this.queryParams.examSubjectId,
        abnormalOption:abnormalOption?abnormalOption:undefined,
        passCard:passCard?passCard:undefined
      }
      this.$prompt(
        "删除考生后，可能造成考生数据异常情况，请输入管理员密码以进行下步操作!",
        "删除全部考生",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入管理员密码",
          inputType: "password",
          inputValidator: (value) => {
            if (!value) {
              return "请输入密码";
            } else {
              return true;
            }
          },
        }
      )
        .then(({ value }) => {
          this.loading = true;
          checkPassword({
            password: value
          })
            .then((res) => {
              scanAlldelete(obj).then((res) => {
                this.$modal.msgSuccess("操作成功");
                this.$refs.tableList.clearSelection();
                this.queryParams= {
                  ...this.queryParams,
                    pageNum: 1,
                    pageSize: 15,
                    examSubjectId: this.$route.query.subjectId
                  }
                  this.tkzt = ''
                this.getList();
              }).finally(() => {
              this.loading = false;
            }).catch(() => {
              this.loading = false;
            });
            })
           
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.el-checkbox.is-bordered {
  height: auto;
}
.top-tabs {
  display: flex;
  padding: 0;
  padding-right: 24px;

  .page-back {
    height: 54px;
    line-height: 54px;
    color: #ffffff;
    background: #1a48db;
    font-size: 14px;
    font-weight: bold;
    margin-right: 24px;
    padding: 0 10px;
    text-align: center;
    cursor: pointer;
    flex-shrink: 0;
  }
}

.img-list > .list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .img-item {
    margin: 0 15px 15px 15px;
    cursor: pointer;

    img {
      display: block;
      width: 360px;
    }
  }
}
</style>